// #define OPENCV_TRAITS_ENABLE_DEPRECATED
#include <opencv2/opencv.hpp>
#include <opencv2/features2d/features2d.hpp>

#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include <math.h>

void MapShiftGenerate()
{
    std::cout << "MapShiftGenerate START" << std::endl;
    cv::Mat mapshow;
    cv::Mat warpAffine_map;
    mapshow = cv::imread("new.png",-1);
    

    float theta = 0.0*M_PI;
    float sintheta;
    float costheta;
    int mov;
    cv::Mat warpGround;
    float angel=0;


    cv::Point2f center(600,600);
    cv::Mat rotMat;
    for(float i=0;i<2;i=i+0.05)
    {
        mov = 300 - i*300;
        theta = i*180;
        rotMat= cv::getRotationMatrix2D(center,theta,1);

        cv::warpAffine(mapshow,warpAffine_map,rotMat,cv::Size(1200,1200));
        warpGround = (cv::Mat_<float>(2,3) << 1, 0, mov,0, 1, mov);
        cv::warpAffine(warpAffine_map,warpAffine_map,warpGround,cv::Size(1200,1200));
        angel = 180*i;
        cv::imwrite("old"+std::to_string(int(angel))+".png",warpAffine_map);
    }
    std::cout << "MapShiftGenerate END" << std::endl;   

}

// void Mapcompare()
// {
//     std::cout<<"MapCompare START"<<std::endl;
//     // 读数据
//     cv::Mat matLeftImage;
//     cv::Mat matRightImage;
//     matLeftImage = cv::imread("new.png",-1);
//     matRightImage = cv::imread("old117.png",-1);

//     // 关键点提取
//     std::vector<cv::KeyPoint> LeftKey;
//     std::vector<cv::KeyPoint> RightKey;
//     cv::Mat LeftDescriptor;
//     cv::Mat RightDescriptor;
//     std::vector<cv::DMatch> Matches;

//     cv::FeatureDetector *pDetector = new SurfFeatureDector;
//     pDetector->detect(matLeftImage,LeftKey);
//     pDetector->detect(matRightImage,RightKey);
//     delete pDetector;

//     cv::FeatureDetector *pExtractor = new SurfDescriptorExtractor;
//     pExtractor->compute(matLeftImage,LeftKey,LeftDescriptor);
//     pExtractor->compute(matRightImage,RightKey,RightDescriptor);
//     delete pExtractor;

//     cv::DescriptorMatcher *pMatcher = new FlannBasedMatcher;
//     pMatcher->match(LeftDescriptor,RightDescriptor,Matches);
//     delete pMatcher;

//     cv::Mat OutImage;
//     cv::drawMatches(matLeftImage,LeftKey,matRightImage,RightKey,Matches,OutImage);
//     cv::imwrite("matchAnswer.png",OutImage);
//     std::cout<<"MapCompare END"<<std::endl;
// }

void Mapcompare()
{
    std::cout<<"MapCompare START"<<std::endl;
    // 读数据
    //
    cv::Mat matLeftImage;
    cv::Mat matRightImage;
    matLeftImage  = cv::imread("new.png",-1);
    matRightImage = cv::imread("old36.png",-1);

    std::vector<cv::KeyPoint> keypoints;
    std::vector<cv::KeyPoint> keypoints_B;

    // 关键点提取
    // 
    cv::Ptr<cv::FastFeatureDetector> fast = cv::FastFeatureDetector::create(40);

    fast->detectAndCompute(matLeftImage,cv::noArray(),keypoints);
    fast->detectAndCompute(matRightImage,cv::noArray(),keypoints_B);

    // 得到了一堆关键点，下面想办法，完成匹配

    // 计算匹配距离？
    // 

    // cv::Mat LeftDescriptor;
    // cv::Mat RightDescriptor;
    // std::vector<cv::DMatch> Matches;
    // cv::DescriptorMatcher *pMatcher;
    // pMatcher->match(LeftDescriptor,RightDescriptor,Matches);

    // cv::drawMatches(matLeftImage,keypoints,matRightImage,keypoints,pMatcher,OutImage);
    // cv::imwrite("FAST_MAP.png",matRightImage);
    std::cout<<"MapCompare END"<<std::endl;
}

int main()
{
    // MapShiftGenerate();
    Mapcompare();
    return 0;
}